package com.ztorn.framework.context;

import com.ztorn.common.assertion.Asserts;
import com.ztorn.common.model.ProcessEntity;
import com.ztorn.common.pool.ProcessPool;

public class ProcessContextHolder {

    private static final ThreadLocal<ProcessEntity> PROCESS_CONTEXT = new ThreadLocal<>();

    public static void setProcess(ProcessEntity process) {
        PROCESS_CONTEXT.set(process);
    }

    public static ProcessEntity getProcess() {
        if (Asserts.isNull(PROCESS_CONTEXT.get())) {
            return ProcessEntity.NULL_PROCESS;
        }
        return PROCESS_CONTEXT.get();
    }

    public static void clear() {
        PROCESS_CONTEXT.remove();
    }

    public static ProcessEntity registerProcess(ProcessEntity process) {
        Asserts.checkNull(process, "Process can not be null.");
        setProcess(process);
        ProcessPool.INSTANCE.put(process.getName(), process);
        return process;
    }
}
